// packageE/appointment/hot_technician/hot_technician.js
const app = getApp();
var location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],

  /**
   * 页面的初始数据
   */
  data: {
    gold: {},
    second: {},
    three: {},
    kwd: "",
    search: false,
    // point: {
    //   lng: 113.25861276415654,
    //   lat: 23.152827312294523
    // },
    // city: "广州市",

    projectList: [],

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    imgPath: app.globalData.imgPath,
    setStyle: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
    this._getLocation((res, e) => {
      this.setData({
        point: res.location,
        city: res.address_component.city,
        appointmentLang: wx.getStorageSync('yz_basic_info').lang.appointment
      })
      if (options.setStyle == 2) {
        this.setData({
          setStyle: 2,
        })
        wx.setNavigationBarTitle({
          title: "附近" + this.data.appointmentLang.worker || ''
        });
      } else {
        wx.setNavigationBarTitle({
          title: "热门" + this.data.appointmentLang.worker || ''
        });
      }
      this.getData();
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bind(e) {
    this.setData({
      kwd: e.detail.value
    });
  },
  init() {
    this.setData({
      kwd: '',
      page: 1,
      isLoadMore: false,
      total_page: 0,
      projectList: [],
      search: false,
      gold: {},
      second: {},
      three: {}
    });
    this.kwd = "";
    this.page = 1;
    this.isLoadMore = false;
    this.total_page = 0;
    this.projectList = [];
    this.search = false;
    this.gold = {};
    this.second = {};
    this.three = {};
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.appointment.frontend.worker.get-workers-by-name");
    let json = {
      lng: this.data.point.lng,
      lat: this.data.point.lat,
      city_name: this.data.city,
      kwd: this.data.kwd,
      page: 1
    }
    if(this.data.setStyle == 2){
      json.is_nearby = 1;
    }
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var response = resdata.data;
        if (this.data.setStyle == 2) {
          this.setData({
            search: true
          })
        } else {
          that.setData({
            search: this.data.kwd ? true : false
          });
        }
        if (response.result == 1) {
          that.setData({
            isLoadMore: true,
            total_page: response.data.last_page,
            projectList: response.data.data,
            gold: response.data.data[0],
            second: response.data.data[1],
            three: response.data.data[2],
          });
          if (!this.data.second) {
            this.setData({
              second: {}
            });
          }
          if (!this.data.three) {
            this.setData({
              three: {
                name: "null",
                has_one_store: {},
                tags: []
              }
            });
          }
          this.setData({
            appointmentLang: wx.getStorageSync('yz_basic_info').lang.appointment
          });
        } else {
          wx.showToast({
            title: response.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 获取更多数据
  getMoreData() {
    this.setData({
      isLoadMore: false
    });
    // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let page = this.data.page + 1;
      this.setData({
        page
      });
      let json = {
        lng: this.data.point.lng,
        lat: this.data.point.lat,
        city_name: this.data.city,
        kwd: this.data.kwd,
        page: this.data.page
      }
      if(this.data.setStyle == 2){
        json.is_nearby = 1;
      }
      let urlStr = app.getNetAddresss('plugin.appointment.frontend.worker.get-workers-by-name');
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          var response = resdata.data;
          if (response.result == 1) {
            var myData = response.data.data.concat(this.data.commentList);
            this.setData({
              commentList: myData
            });
          } else {
            wx.showToast({
              title: response.msg,
              icon: 'none'
            });
            let pages = this.data.page - 1;
            this.setData({
              page: pages,
              isLoadMore: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  goBack() {
    let pages = getCurrentPages();
    if (pages.length <= 1) {
      wx.reLaunch({
        url: '/packageG/index/index',
      });
    } else {
      wx.navigateBack({ //返回
        delta: 1
      });
    }
  },
  gotoWorker(e) {
    let id = e.currentTarget.dataset.id;
    let point = JSON.stringify(this.data.point);
    wx.navigateTo({
      url: '/packageE/appointment/technician_details/technician_details?worker_id=' + id + '&point=' + point,
    });
  },
});